package vn.lumi.smarthome.network;

import vn.lumi.smarthome.LumiHome;
import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_DeviceScene;
import vn.lumi.smarthome.io.database.TBL_DeviceState;
import vn.lumi.smarthome.io.database.TBL_Scene;
import vn.lumi.smarthome.ulti.Preference;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Parcel;

public class DelScene extends AppMsg {

	// constants

	// attributes
	private int[] sceneIDs = null;
	private int newConfigVersion;

	public DelScene(int Msg_id, int uid, int sid, int[] SceneIDs) {
		super(NetMessage.CMD_APP_DEL_SCENE, Msg_id, uid, sid);
		// TODO Auto-generated constructor stub
		sceneIDs = new int[SceneIDs.length];
		for (int i = 0; i < SceneIDs.length; i++) {
			sceneIDs[i] = SceneIDs[i];
		}
	}

	@Override
	public byte[] generateByteArray() {
		// TODO Auto-generated method stub
		byte[] result = new byte[5 + sceneIDs.length];
		result[0] = (byte) this.Cmd_id;
		result[1] = (byte) this.Msg_id;
		result[2] = (byte) this.user_id;
		result[3] = (byte) this.session_id;
		result[4] = (byte) sceneIDs.length;
		for (int i = 0; i < sceneIDs.length; i++) {
			result[5 + i] = (byte) sceneIDs[i];
		}
		return result;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeInt(this.Cmd_id);
		dest.writeInt(this.length);
		dest.writeInt(this.Msg_id);
		dest.writeInt(this.session_id);
		dest.writeInt(this.user_id);
		dest.writeIntArray(sceneIDs);

	}

	@Override
	public void doIfSendSuccessful(Context context) {
		// TODO Auto-generated method stub
		// del Scene
		for (int sceneID : sceneIDs) {
			String selection = new String(TBL_Scene._ID + "=?");
			String[] selectionArgs = new String[] { sceneID + "" };
			context.getContentResolver().delete(TBL_Scene.CONTENT_URI_SCENE,
					selection, selectionArgs);

			// find state ids
			selection = new String(TBL_DeviceScene.SCENE_ID + "=?");
			String[] projection = new String[] { TBL_DeviceScene.STATE_ID };
			Cursor cursor = context.getContentResolver().query(
					TBL_DeviceScene.CONTENT_URI_DEVICE_SCENE, projection,
					selection, selectionArgs, null);
			if (cursor.getCount() == 0) {
				if (cursor != null) {
					cursor.close();
					cursor = null;
				}
				continue;
			}

			for (cursor.moveToFirst(); cursor.isAfterLast(); cursor
					.moveToNext()) {
				int stateID = cursor.getInt(cursor
						.getColumnIndex(TBL_DeviceScene.STATE_ID));
				String selection1 = new String(TBL_DeviceState._ID + "=?");
				String[] selectionArgs1 = new String[] { stateID + "" };
				// del device state
				context.getContentResolver().delete(
						TBL_DeviceState.CONTENT_URI_DEVICE_STATE, selection1,
						selectionArgs1);
			}
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
			// del scene device
			context.getContentResolver().delete(
					TBL_DeviceScene.CONTENT_URI_DEVICE_SCENE, selection,
					selectionArgs);
		}

		// save config in preferences
		LumiHome.HC_Config_ver = this.newConfigVersion;
		Preference.saveConfigVersion(context, this.newConfigVersion);

		// inform UI
		Intent intent = new Intent();
		intent.setAction(context
				.getString(R.string.txt_vn_lumi_smarthome_del_scenes_successful));
		context.sendBroadcast(intent);
	}

	@Override
	public void doIfSendFail(Context context) {
		// TODO Auto-generated method stub
		// inform UI
		Intent intent = new Intent();
		intent.setAction(context
				.getString(R.string.txt_vn_lumi_smarthome_del_scenes_failed));
		context.sendBroadcast(intent);
	}

	@Override
	public void readFromParcel(Parcel in) {
		// TODO Auto-generated method stub
		Cmd_id = in.readInt();
		length = in.readInt();
		Msg_id = in.readInt();
		session_id = in.readInt();
		user_id = in.readInt();
		in.readIntArray(sceneIDs);
	}

	@Override
	public boolean isCorrectReply(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result = false;
		AckScene ack = new AckScene();
		if (ack.isCorrectFormat(input, length)) {
			ack.parseMessage(input, length);
			if (ack.getMsg_id() == this.Msg_id
					&& ack.getUserId() == this.getUid()) {
				this.newConfigVersion = ack.getConfigVersion();
				result = true;
			}
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.AppMsg#isCorrectBusy(byte[], int)
	 */
	@Override
	public boolean isCorrectBusy(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result = false;
		HC_ACK_BUSY ack_busy = new HC_ACK_BUSY();
		if (ack_busy.isCorrectFormatBusy(input, length)) {
			ack_busy.parseMessage(input, length);
			if (ack_busy.getMsg_id() == this.Msg_id)
				result = true;
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.AppMsg#doIfHcBusying()
	 */
	@Override
	public void doIfHcBusying() {
		// TODO Auto-generated method stub

	}

}
